package com.zxt.content.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket apiUser() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户接口文档")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxt.content.user.controller"))
                .build()
                .apiInfo(apiInfo()).enable(Boolean.TRUE);
    }

    @Bean
    public Docket apiThirdParty() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("第三方接口文档")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxt.content.thirdParty.controller"))
                .build()
                .apiInfo(apiInfo()).enable(Boolean.TRUE);
    }

    @Bean
    public Docket apiCopywriting() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("文案接口文档")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxt.content.copywriting.controller"))
                .build()
                .apiInfo(apiInfo()).enable(Boolean.TRUE);
    }

    @Bean
    public Docket apiQuestion() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("问题接口文档")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxt.content.question.controller"))
                .build()
                .apiInfo(apiInfo()).enable(Boolean.TRUE);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("对外开放接口API文档").description("对外开放接口API文档")
                .version("1.0").license("LICENSE").build();
    }

    /**
     * swagger ui资源映射
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * swagger-ui.html路径映射，浏览器中使用/api-docs访问
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/api-docs", "/doc.html");
    }

}
